﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;

namespace Lamex.mLabor.UI
{
    public partial class frmLBR_WorkingTime_Planning : frmBaseChild
    {
        string strSql;
        DataTable dt;
        string strSub_Factory, strPlanning_Date;

        public frmLBR_WorkingTime_Planning()
        {
            InitializeComponent();
        }

        private void frmLBR_WorkingTime_Planning_Load(object sender, EventArgs e)
        {
//            SELECT SUB_FACTORY,tb1.WORKSTATION,PROCESS_TIME AS 排产工时,OT_LENGTH AS 参考工时,OT_LENGTH AS 计划加班
//FROM
//(SELECT SUB_FACTORY,WORKSTATION,OT_LENGTH
//FROM OP_MGT_Factory_Workstation AS tb1_a
//LEFT JOIN 
//(SELECT OT1_LOCATION,SUM(OT1_LENGTH) AS OT_LENGTH
//FROM HR_Labor_Shift_Actual 
//GROUP BY OT1_LOCATION) AS tb1_b
//ON tb1_a.WORKSTATION=tb1_b.OT1_LOCATION) tb1
//LEFT JOIN 
//(SELECT WORKSTATION,SUM(PROCESS_TIME) AS PROCESS_TIME
//FROM 
//(SELECT WORKSTATION,tb2_a.PROCESS_CODE,PROCESS_NAME,
//(PROCESS_TIME*tb2_b.ALLOWANCE_FACTOR)/3600 AS PROCESS_TIME
//FROM View_OP_Follow_Process_Time AS tb2_a
//LEFT JOIN IE_WorkCenter_AllowanceFactor AS tb2_b
//ON tb2_a.PROCESS_CODE=tb2_b.PROCESS_CODE) AS tb2_c
//GROUP BY WORKSTATION) AS tb2
//ON tb1.WORKSTATION=tb2.WORKSTATION
        }

        private void sbtnRefresh_Click(object sender, EventArgs e)
        {
            strSub_Factory = cboxSub_Factory.Text.ToString();
            strPlanning_Date = dtePlanning_Date.Text.ToString();

            if (strSub_Factory == "" || strPlanning_Date == "")
            {            
                //MessageBox.Show("分厂及计划日期不能为空！", "加班计划刷新提示");
                DevExpress.XtraEditors.XtraMessageBox.Show("分厂及计划日期不能为空！", "加班计划刷新提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            refreshDetailGridview();
            refreshAnalyzeGridview();



        }

        private void refreshDetailGridview()
        {
            //获取最近工作日实际加班工时，作为建议加班工时申报，用户可在前台进行调整
            strSql = @"SELECT WORKSTATION,WORKER_NO,OT_LENGTH
FROM 
(SELECT SUB_FACTORY,WORKSTATION,WORKER_NO,OT_LENGTH
FROM 
(SELECT WORKER_NO,OT_LOCATION,OT_LENGTH
FROM HR_Labor_Shift_Actual
WHERE SHIFT_DATE=(SELECT MAX(SHIFT_DATE) FROM HR_Labor_Shift_Actual)) AS a
LEFT JOIN 
OP_MGT_Factory_Workstation AS b
ON a.OT_LOCATION=b.WORKSTATION) AS c
WHERE SUB_FACTORY='" + strSub_Factory + @"'";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            if (dt.Rows.Count > 0)
            {
                gridControl1.DataSource = dt;
            }
        }

        private void refreshAnalyzeGridview()
        {
            dt = ((DataView)gridView1.DataSource).ToTable();

            //对明细表进行求合
            //此为测试SQL，正式上线【计划加班工时】数据绑定gridView1（用户修改后数据）
            string strSql = @"SELECT f.WORKSTATION AS 工作站,PROCESS_TIME AS 排产欠货工时,OT_LENGTH AS 计划加班工时
FROM
(SELECT WORKSTATION,OT_LENGTH
FROM OP_MGT_Factory_Workstation AS a
LEFT JOIN 
(SELECT OT_LOCATION,SUM(OT_LENGTH) AS OT_LENGTH
FROM HR_Labor_Shift_Actual
WHERE SHIFT_DATE=(SELECT MAX(SHIFT_DATE) FROM HR_Labor_Shift_Actual)
GROUP BY OT_LOCATION) AS b
ON a.WORKSTATION=b.OT_LOCATION
WHERE SUB_FACTORY='" + strSub_Factory + @"') AS f
LEFT JOIN
(SELECT WORKSTATION,SUM(PROCESS_TIME)/3600 AS PROCESS_TIME
FROM
(SELECT WORKSTATION,c.PROCESS_CODE,PROCESS_NAME,
PROCESS_TIME*d.ALLOWANCE_FACTOR AS PROCESS_TIME
FROM View_OP_Follow_Process_Time AS c
LEFT JOIN IE_WorkCenter_AllowanceFactor AS d
ON c.PROCESS_CODE=d.PROCESS_CODE) AS e
GROUP BY WORKSTATION) AS g
ON f.WORKSTATION=g.WORKSTATION";

            dt = Lamex.Base.BaseBuildSql.GetDataTableBySql(strSql);
            vGridControl1.DataSource = dt;

            //if (dataTable.Rows.Count > 0)
            //{
            //重定义表列名，便于与SAP对接
            //for (int i = 0; i < dataTable.Rows.Count; i++)
            //{
            //DataRow dr = dt_MES.NewRow();
            //dr["AUFNR"] = dataTable.Rows[i]["MO_No"].ToString();
            //dr["MATNR"] = dataTable.Rows[i]["物料编号"].ToString();
            //dr["BUDAT"] = Convert.ToDateTime(dataTable.Rows[i]["生产日期"]).ToString("yyyy-MM-dd");
            //dt_MES.Rows.Add(dr);
            //    }
            //}
        }

        private void cboxFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            clearDataSource();
        }

        private void dteSelDate_EditValueChanged(object sender, EventArgs e)
        {
            clearDataSource();
        }

        private void clearDataSource()
        {
            gridControl1.DataSource = null;
            vGridControl1.DataSource = null;
            chartControl1.DataSource = null;

        }
    }
}